package T5_why_synchronized;

public class Why_synchronized implements Runnable {    
    private Foo foo = new Foo();    
    public static void main(String[] args) {    
    	Why_synchronized r = new Why_synchronized();    
        Thread ta = new Thread(r, "Thread-A");    
        Thread tb = new Thread(r, "Thread-B");    
        ta.start();    
        tb.start();    
    }    
    public void run() {    
        for (int i = 0; i < 3; i++) {    
            this.fix(30);    
            try {    
                Thread.sleep(10);    
            } catch (InterruptedException e) {    
                e.printStackTrace();    
            }    
            System.out.println(Thread.currentThread().getName() + " x = " + foo.getX());    
        }    
    }    
    public int fix(int y) {    
        return foo.fix(y);    
    }    
}  
class Foo {    
    private int x = 100;    
    public int getX() {    
        return x;    
    }    
    public int fix(int y) {    
        x = x - y;    
        return x;    
    }    
}  
